package com.example.housesrv.controller;

import com.example.housesrv.common.CommonConstants;
import com.example.housesrv.common.HouseUserType;
import com.example.housesrv.common.RestCode;
import com.example.housesrv.common.RestResponse;
import com.example.housesrv.model.House;
import com.example.housesrv.model.HouseUserReq;
import com.example.housesrv.service.HouseService;
import com.google.common.base.Objects;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author ：Anitaxiaofeng
 * @description：
 * @create 2018-09-17
 **/
@RequestMapping("house")
@RestController
public class HouseController {


    @Autowired
    private HouseService houseService;

    @RequestMapping("add")
    public RestResponse<Object> doAdd(@RequestBody House house) {
        house.setState(CommonConstants.HOUSE_STATE_UP);
        // house 的 userId 在api-gateway中添加
        if(house.getUserId() == null) {
            return RestResponse.error(RestCode.ILLEGAL_PARAMS);
        }
        houseService.addHouse(house,house.getUserId());
        return RestResponse.success();
    }

    /**
     * 收藏/取消收藏
     **/
    @RequestMapping("bind")
    public RestResponse<Object> delsale(@RequestBody HouseUserReq req) {
        Integer bindType = req.getBindType();
        HouseUserType houseUserType = Objects.equal(bindType, 1) ? HouseUserType.SALE : HouseUserType.BOOKMARK;
        if(req.isUnBind()) {
            houseService.unbindUser2House(req.getHouseId(),req.getUserId(),houseUserType);
        } else {
            houseService.bindUser2House(req.getHouseId(),req.getUserId(), houseUserType);
        }
        return RestResponse.success();
    }
}
